Flights on Coastal Airways arrive in the late afternoon and depart early the next morning back to Dar es Salaam, therefore you MUST spend the night.
Driving can be very difficult in the rainy season.
Buses run daily from both the north and south. From the north Dar big buses leave around 6am from Ubungo and stop at Temeke and Mbagala bus stations. The companies running change regularly, the buses with four seats in a row are significantly more comfortable than the ones with five seats in a row. Part of the road is unpaved so it is advisable to get a seat nearer the front. It's about 4 or 5 hours from Dar. It's best to book at least a day in advance. These buses will drop you off at Nangurukuru which is about 30km from Kilwa Masoko where most people stay. From Nangurukuru you can get a minibus, shared taxi or private taxi to either Kilwa Masoko or Kilwa Kivinje.
From Dar there are also minibuses throughout the morning leaving from Mbagala to Kilwa Masoko although these will be more cramped and less comfortable than the larger buses.
From the south Mtwara, Mikindani and Lindi buses depart both towns at 6am and it takes about 4 or 5 hours, the road is paved all the way. These buses will also drop you off at Nangurukuru see above.
From Lindi but not Mtwara there are also minibuses in the morning. As around at the bus stand to find out what time they leave.